/*
 * ExportTableJDialog.java
 *
 * Created on 03.07.2009, 15:09:32
 */

package morenoapp.components.common;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import javax.swing.table.TableModel;
import org.jdesktop.application.Action;

/**
 *
 * @author dmitry.mamonov
 */
public class ExportTableJDialog extends javax.swing.JDialog {

    /** Creates new form ExportTableJDialog
     * @param parent
     * @param modal 
     */
    public ExportTableJDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane = new javax.swing.JScrollPane();
        jTextArea = new javax.swing.JTextArea();
        jButtonClose = new javax.swing.JButton();
        jButtonCopyToClipboard = new javax.swing.JButton();
        jLabelTip = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane.setName("jScrollPane"); // NOI18N

        jTextArea.setColumns(20);
        jTextArea.setRows(5);
        jTextArea.setName("jTextArea"); // NOI18N
        jScrollPane.setViewportView(jTextArea);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(morenoapp.MorenoApp.class).getContext().getActionMap(ExportTableJDialog.class, this);
        jButtonClose.setAction(actionMap.get("actionClose")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(morenoapp.MorenoApp.class).getContext().getResourceMap(ExportTableJDialog.class);
        jButtonClose.setText(resourceMap.getString("jButtonClose.text")); // NOI18N
        jButtonClose.setName("jButtonClose"); // NOI18N

        jButtonCopyToClipboard.setAction(actionMap.get("actionCopyToClipboard")); // NOI18N
        jButtonCopyToClipboard.setText(resourceMap.getString("jButtonCopyToClipboard.text")); // NOI18N
        jButtonCopyToClipboard.setName("jButtonCopyToClipboard"); // NOI18N

        jLabelTip.setText(resourceMap.getString("jLabelTip.text")); // NOI18N
        jLabelTip.setName("jLabelTip"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonClose))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabelTip, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCopyToClipboard))
            .addComponent(jScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 683, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelTip)
                    .addComponent(jButtonCopyToClipboard))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonClose))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonClose;
    private javax.swing.JButton jButtonCopyToClipboard;
    private javax.swing.JLabel jLabelTip;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JTextArea jTextArea;
    // End of variables declaration//GEN-END:variables


    public void setTextToExport(String text){
        jTextArea.setText(text);
    }

    public void setTableModelToExport(TableModel model){
        StringBuilder text = new StringBuilder();

        int rows = model.getRowCount();
        int columns = model.getColumnCount();

        for(int r=0;r<rows;r++){
            for(int cl=0;cl<columns;cl++){
                text.append(model.getValueAt(r, cl)).append("\t"); //NOI18N
            }
            text.append("\n"); //NOI18N
        }
        setTextToExport(text.toString());
    }

    /**
     * TODO: учитывать десятичный разделителья для разных локалей.
     */
    @Action
    public void actionCopyToClipboard() {
        jTextArea.selectAll();
        String selection = jTextArea.getSelectedText();
        StringSelection data = new StringSelection(selection);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(data, data);
        jTextArea.selectAll();
    }

    @Action
    public void actionClose() {
        this.setVisible(false);
    }
}
